package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.ActivityModel.ACTIVITY_MODEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.ModuleContentItem.MODULE_CONTENT_ITEM;

import java.sql.Timestamp;
import java.util.List;

import com.starbaba.tools.sequence.Sequence;
import org.jooq.Record;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.entity.family.ActivityModel;
import com.tuanzi.loan.business.repository.family.custom.ActivityModelRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.generated.jooq.loan.tables.records.ActivityModelRecord;
import com.tuanzi.loan.vo.request.family.ActivityModelSearchRequest;
import com.tuanzi.loan.vo.response.family.ActivityModelSearchResponse;
import com.tuanzi.loan.vo.view.family.ActivityModelView;

@Repository
public class ActivityModelRepositoryImpl extends JOOQBaseRepositoryPlus implements ActivityModelRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return ACTIVITY_MODEL;
	}

	@Override
	public Page<ActivityModelSearchResponse> list(ActivityModelSearchRequest searchRequest, Pageable pageable) {
		fields(ACTIVITY_MODEL.fields());
		contains(ACTIVITY_MODEL.MODELNAME, searchRequest.getModelName());
		contains(ACTIVITY_MODEL.MODELREMARK, searchRequest.getModelRemark());
		eq(ACTIVITY_MODEL.MODELTYPE, searchRequest.getModelType());
		orderBy(ACTIVITY_MODEL.CTIME, Sort.Direction.DESC);
		return search(pageable, (record) -> {
			ActivityModelSearchResponse response = new ActivityModelSearchResponse();
			response.setId(record.get(ACTIVITY_MODEL.ID));
			response.setModelName(record.get(ACTIVITY_MODEL.MODELNAME));
			response.setModelRemark(record.get(ACTIVITY_MODEL.MODELREMARK));
			response.setModelType(record.get(ACTIVITY_MODEL.MODELTYPE));
			response.setTargetUrl(record.get(ACTIVITY_MODEL.TARGET_URL));
			response.setCreateTime(record.get(ACTIVITY_MODEL.CTIME));
			return response;
		});
	}

	@Override
	public ActivityModel findOneById(Long id) {
		return dslContext.select(ACTIVITY_MODEL.fields()).from(ACTIVITY_MODEL).where(ACTIVITY_MODEL.ID.eq(id)).fetchOne((this::getActivityModel));
	}

	@Override
	public ActivityModelRecord saveOrUpdate(ActivityModelView view) {
		ActivityModelRecord record = dslContext.newRecord(ACTIVITY_MODEL);
		record.setmodelName(view.getModelName());
		record.setmodelRemark(view.getModelRemark());
		record.setmodelType(view.getModelType());
		record.settarget_url(view.getTargetUrl());

		if (null == view.getId()) {
			Long modelId = Sequence.getSequence();
			record.setid(modelId);
			record.setctime(new Timestamp(System.currentTimeMillis()));
			record.store();
		} else {
			record.setid(view.getId());
			record.update();
		}

		return record;
	}

	@Override
	public ActivityModel findOneByModelName(String modelName) {
		return dslContext.select(ACTIVITY_MODEL.fields()).from(ACTIVITY_MODEL).where(ACTIVITY_MODEL.MODELNAME.eq(modelName)).fetchOne((this::getActivityModel));
	}

	@Override
	public List<ActivityModel> findAllActivityModel() {
		return dslContext.select(ACTIVITY_MODEL.fields()).from(ACTIVITY_MODEL).where("1 = 1").fetch((this::getActivityModel));
	}

	@Override
	public void deleteModel(Long id) {
		dslContext.delete(ACTIVITY_MODEL).where(ACTIVITY_MODEL.ID.eq(id)).execute();
	}

	private ActivityModel getActivityModel(Record record) {
		ActivityModel model = new ActivityModel();
		model.setId(record.get(ACTIVITY_MODEL.ID));
		model.setModelName(record.get(ACTIVITY_MODEL.MODELNAME));
		model.setModelRemark(record.get(ACTIVITY_MODEL.MODELREMARK));
		model.setModelType(record.get(ACTIVITY_MODEL.MODELTYPE));
		model.setTargetUrl(record.get(ACTIVITY_MODEL.TARGET_URL));
		model.setCreateTime(record.get(ACTIVITY_MODEL.CTIME));
		return model;
	}

}
